<?php
class VsCampania extends Controller
{
	private $_semanaArray;
	
	public function __construct()
	{
		parent::Controller();
		$this->load->model(array('vistacampaniamodel','mmotivosalida'));
		$this->load->library(array('chgraficos','chcalendario'));
	}
	
	public function index()
	{
		$this->load->view('main', array('content_page' => 'campaninbound_list', 'menutop' => '0'));
	}
	
	public function data($filtro = false)
	{
		 $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'IdTransaccion'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;

		 if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		      
		      $fechaA  = false;
		      $fechaF = false;
		       
		   	  if($filtro != false) {
		   	  	
		   	  	  $filtro = explode('-', $filtro);
		   	  	  
		   	  	  if (strlen($filtro[0]) > 3) {
		   	  	  	 $searchA = explode('.',$filtro[0]);
		   	         $fechaA = $searchA[2].'-'.$searchA[0].'-'.$searchA[1];
		   	  	  }
		   	  	
		   	  	  if (strlen($filtro[1]) > 3) {
		   	  	  	 $searchF = explode('.', $filtro[1]);
		      	     $fechaF = $searchF[2].'-'.$searchF[0].'-'.$searchF[1].' 59:59:59';
		   	  	  }
		   	      
		      } 
		       
	     $records = $this->vistacampaniamodel->loadData($start, $limit, $sortname, $sortorder, $fechaA, $fechaF);
			
		 $totalRecords = count($records);
		
		 $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){
			    
			  		 if ($objetos->getIdMotivoSalida() == 0) {
			  		 	$salida = "Activo";
			  		 }else{
			  		    $salida = $this->mmotivosalida->LoadEstatusById($objetos->getIdMotivoSalida());
			  		    $salida = $salida->getNombre();
			  		 }
			  		 
				     $arr = $json_output['rows'][] = array(
				          'Inbound'            => $objetos->getIdTransaccion(),
				          'Gestor'             => $objetos->getReferencia(),
				     	  'Fecha'              => $objetos->getFecha(),
				     	  'Codigo'             => $objetos->getCampania(),
				     	  'tipoS'              => $salida
				     
				      );
			      }
		      }

		 echo json_encode($json_output);
	}
	
	public function generarXmls()
	{
		$data = $this->vistacampaniamodel->loadData();
		
		$documento = "";
		
		foreach ($data as $record){
			
		       if ($record->getIdMotivoSalida() == 0) {
			  		 	$salida = "Activo";
			  		 }else{
			  		    $salida = $this->mmotivosalida->LoadEstatusById($record->getIdMotivoSalida());
			  		    $salida = $salida->getNombre();
			  	}
			
			$documento .= "<table>
							<tr>
								<td>'".$record->getIdTransaccion()."'</td>
								<td>'".$record->getReferencia()."'</td>
								<td>'".$record->getFecha()."'</td>
								<td>'".$record->getCampania()."'</td>
								<td>'".$salida."'</td>
							</tr>
			               </table>";
		}
		
	    $csvName = "campania.xls";
	   
	    $ar = fopen($csvName,"a") or die("Fallo");
        fwrite($ar, $documento);
        fclose($ar);
       
        header('Content-type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=campania.xls");
        header('Last-Modified: '.date('D, d M Y H:i:s'));
        header("Pragma: no-cache");
        header("Expires: 0");
        
        echo($documento); 
      
	}	
}